import ArrayUtil from '@/ArrayUtil';
import BaseUtil from '@/BaseUtil';
import Buff from '@/Buff';
import BuffUtil from '@/BuffUtil';
import BuffCommon from 'code/Core/Common/BuffCommon';
import SelectCommon from 'code/Core/Common/SelectCommon';
import SummonCommon from 'code/Core/Common/SummonCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 共生图腾
 **/
export default class SM_TogatherToten extends AbilityBase {
    static instance: SM_TogatherToten;
    buffType: AppBuffType = {
        class: '光环',
        id: 'buff_SM_TogatherToten',
        art: this.art,
        name: this.abilityName,
        ubertip: '受到治疗并所有单位共享生命',
        dur: 1.5,
        onDestroy: this.buffDestroy.bind(this),
    };
    buffDestroy(buff: Buff) {
        ArrayUtil.removeElement(this.buffEffectUnits, buff.getParent());
    }
    totenBuffType: AppBuffType = {
        id: 'buff_SM_TogatherToten_toten',
        art: this.art,
        name: this.abilityName,
        ubertip: '',
        dur: 10,
        interval: 0.5,
        onInterval: this.buffInterval.bind(this),
    };
    buffInterval(buff: Buff) {
        this.buffEffect(buff.getParent(), buff.getCaster());
    }
    buffEffectUnits: unit[] = [];
    constructor(id: string = 'Agst') {
        super(id);
        SM_TogatherToten.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
        EventManager.onTakeDamage(this.onDamageEffect.bind(this));
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
        BuffUtil.registerBuffType(this.totenBuffType);
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        let dur = 8 + this.getAbilityLevel(source) * 2;
        let [uv] = SummonCommon.summon({
            x: tarX,
            y: tarY,
            unitTypeId: 'vgst',
            caster: source,
            lifeTime: dur,
        });
        BuffCommon.addBuff({
            target: uv,
            caster: source,
            buffTypeId: this.totenBuffType.id,
            ablityId: this.id,
            config: { dur },
        });
        this.buffEffect(uv, source);
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
    buffEffect(uv: unit, caster: unit) {
        let us = SelectCommon.getAllyUnitInRangeExcludeCenterUnit(uv, 450);
        let lv = this.getAbilityLevel(caster);
        let dmg = 40 + UnitCommon.取法强(caster) * (1 + 0.5 * lv);
        for (let u of us) {
            BuffCommon.addBuff({
                target: u,
                caster,
                buffTypeId: this.buffType.id,
                ablityId: this.id,
            });
            DamageSystemInstance.applyHeal(u, caster, dmg / 2, this.id);
            DestroyEffect(AddSpecialEffectTarget('Abilities\\Spells\\Undead\\VampiricAura\\VampiricAuraTarget.mdl', u, 'overhead'));
            if (!this.buffEffectUnits.includes(u)) {
                this.buffEffectUnits.push(u);
            }
        }
    }
    onDamageEffect(d: DamageEventData) {
        if (d.isStop) return;
        if (!this.buffEffectUnits.includes(d.target)) return;
        if (!IsUnitAliveBJ(d.target)) return;
        if (!BuffUtil.hasUnitBuff(d.target, this.buffType.id)) return;
        let dmg = d.lastDamage / this.buffEffectUnits.length;
        d.lastDamage = d.initDamage = 0;
        for (let u of this.buffEffectUnits) {
            let p = { ...d };
            p.target = u;
            p.isStop = true;
            DamageSystemInstance.applyDamage(u, d.source, dmg, p);
        }
    }
}
